
package com.jf.cloud.service.sysmanager.remote.api;

import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.taglib.Paging;
import com.jf.cloud.service.sysmanager.dao.IPubgMsgLogService;
import com.jf.cloud.service.sysmanager.entity.PubgMsgLog;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.web.BaseController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户消息控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/appPubgMsgLog")
public class AppPubgMsgLogController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(AppPubgMsgLogController.class);

	@Resource
	private TokenHandler tokenHandler;
	
	@Resource
	private IPubgMsgLogService pubgMsgLogService;

	/**
	 * 用户消息列表
	 *
	 * @author 高冬冬 创建时间：2018-04-09
	 */
	@ResponseBody
	@RequestMapping("/msgLogList")
	public Map<String, Object> msgLogList(OPaging<PubgMsgLog> page, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", -1);
		result.put("message", "获取用户消息失败！");
		try {
			String token = getParamValueString(request, "token");
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.put("code", -2);
				result.put("message", "请重新登陆！");
				return result;
			}
			// 每页数据条数
			int limit = getParamValueInteger(request, "limit", 50);
			page.setLimit(limit);

			// 第几页
			int offset = getParamValueInteger(request, "offset", -1);
			if (offset > 0) {
				page.setOffset((offset-1)*limit);
			}

            //返回的数据集合
			Map<String,String> columns = new HashMap<String,String>();
			columns.put("ROW_ID", "rowId");
			columns.put("SMALL_NAME", "smallName");
			columns.put("CONTENT", "content");
			columns.put("CREATE_TIME", "createTime");
            page.getWm().put("USER_ID", userId);
			page.setOrderBy(" ORDER BY CREATE_TIME DESC ");
            Paging<PubgMsgLog> pageTemp = pubgMsgLogService.findPage(columns, page);
            result.put("data", pageTemp.getDataList());
            result.put("code", 0);
            result.put("message", "获取用户消息成功！");
		} catch (Exception e) {
			log.error("获取用户消息出错！", e);
		}
		return result;
	}
}
